<?php

/*
  Quan Van Sinh
  sinhquan@vietnambiz.com
 */

class question {

    function question() {

    }

    function getAllQuestion($limit=10) {
        global $db, $config;
        if($_POST['status']=='yes'){
            $_SESSION['questionStatusFilter'] = 'yes';
        }elseif($_POST['status']=='no'){
            $_SESSION['questionStatusFilter'] = 'no';
        }  elseif ($_POST['status']=='all') {
            unset ($_SESSION['questionStatusFilter']);
        }
        
        $query_condition = "";
        if($_SESSION['questionStatusFilter']=='yes'){
            $query_condition .= " AND active=1 ";
        }elseif($_SESSION['questionStatusFilter']=='no'){
            $query_condition .= " AND active=0 ";
        }
        
        if(isset($_GET['keyword']) && trim($_GET['keyword'])!=''){
            $keyword = $_GET['keyword'];
            $query_condition .= " AND (title LIKE '%$keyword%' OR question LIKE '%$keyword%') ";
        }
        
        $sqlTotalRows = "SELECT count(*) as total FROM question WHERE 1 $query_condition";
        $rsTotal = $db->query_first($sqlTotalRows);
        $totalRows = $rsTotal['total'];
        $totalPages = ceil($totalRows / $config ['total_article_on_admin']);
        $start = (($config ['page'] * $config ['total_article_on_admin']) - $config ['total_article_on_admin']);
        $SQL = "SELECT * FROM question WHERE 1 $query_condition ORDER BY last_answer DESC,create_date DESC LIMIT $start,$limit";
        $rsPost = $db->query($SQL);
        return array('rs' => $rsPost, 'totalPage' => $totalPages);
    }

    function getFileAttach($question_id) {
        global $db, $config;
        $SQL = "SELECT * FROM question_attach WHERE question_id=$question_id";
        $rs = $db->query_first($SQL);
        return $rs;
    }

    function action() {
        global $config;
        global $db;
        if (isset($_REQUEST['action'])) {
            $ids = $_POST['multiId'];
            switch ($_REQUEST['action']) {
                case 'delete':
                    if (count($ids) > 0) {
                        for ($i = 0; $i < count($ids); $i++) {
                            $this->deleteQuestion(intval($ids[$i]));
                        }
                        $_SESSION['rsMessages'] = '<span class="success">Delete the question successfull!</span>';
                    } else {
                        $_SESSION['rsMessages'] = '<span class="error">No row select!</span>';
                    }
                    break;
                case 'active_yes':
                    for ($i = 0; $i < count($ids); $i++) {
                        $db->query("UPDATE question set active=1 WHERE id=" . intval($ids[$i]));
                    }
                    $_SESSION['rsMessages'] = '<span class="success">Change active to yes successfull!</span>';
                    break;
                case 'active_no':
                    for ($i = 0; $i < count($ids); $i++) {
                        $db->query("UPDATE question set active=0 WHERE id=" . intval($ids[$i]));
                    }
                    $_SESSION['rsMessages'] = '<span class="success">Change active to no successfull!</span>';
                    break;
                case 'hot_yes':
                    for ($i = 0; $i < count($ids); $i++) {
                        $db->query("UPDATE question set hot=1 WHERE id=" . intval($ids[$i]));
                    }
                    $_SESSION['rsMessages'] = '<span class="success">Change hot to yes successfull!</span>';
                    break;
                case 'hot_no':
                    for ($i = 0; $i < count($ids); $i++) {
                        $db->query("UPDATE question set hot=0 WHERE id=" . intval($ids[$i]));
                    }
                    $_SESSION['rsMessages'] = '<span class="success">Change hot to no successfull!</span>';
                    break;
                case 'top_yes':
                    for ($i = 0; $i < count($ids); $i++) {
                        $db->query("UPDATE question set top=1 WHERE id=" . intval($ids[$i]));
                    }
                    $_SESSION['rsMessages'] = '<span class="success">Change top to yes successfull!</span>';
                    break;
                case 'top_no':
                    for ($i = 0; $i < count($ids); $i++) {
                        $db->query("UPDATE question set top=0 WHERE id=" . intval($ids[$i]));
                    }
                    $_SESSION['rsMessages'] = '<span class="success">Change top to no successfull!</span>';
                    break;
                case 'delete_answer':
                    if (count($ids) > 0) {
                        for ($i = 0; $i < count($ids); $i++) {
                            $this->deleteAnswer(intval($ids[$i]));
                        }
                        $_SESSION['rsMessages'] = '<span class="success">Delete the answer successfull!</span>';
                    } else {
                        $_SESSION['rsMessages'] = '<span class="error">No row select!</span>';
                    }
                    header("Location: " . $_SERVER['REQUEST_URI']);
                    die;
                    break;
                case 'active_yes_answer':
                    if (count($ids) > 0) {
                        for ($i = 0; $i < count($ids); $i++) {
                            $db->query("UPDATE answer set active=1 WHERE id=" . intval($ids[$i]));
                        }
                        $_SESSION['rsMessages'] = '<span class="success">Change active to yes successfull!</span>';
                    } else {
                        $_SESSION['rsMessages'] = '<span class="error">No row select!</span>';
                    }
                    header("Location: " . $_SERVER['REQUEST_URI']);
                    die;
                    break;
                case 'active_no_answer':
                    if (count($ids) > 0) {
                        for ($i = 0; $i < count($ids); $i++) {
                            $db->query("UPDATE answer set active=0 WHERE id=" . intval($ids[$i]));
                        }
                        $_SESSION['rsMessages'] = '<span class="success">Change active to no successfull!</span>';
                    } else {
                        $_SESSION['rsMessages'] = '<span class="error">No row select!</span>';
                    }
                    header("Location: " . $_SERVER['REQUEST_URI']);
                    die;
                    break;
                case 'viewQuestion':
                    include_once $config ['admin_document_root'] . "/modules/question_detail.php";
                    die;
                case 'updateQuestion':
                    if ($_POST['updateQuestion'] == 1) {
                        $id = trim($_GET['id']);
                        $title = filter($_POST['title']);
                        $question = trim($_POST['question']);
                        $link = CleanFileName($title).'-'.$id;
                        $cat_id = intval($_POST['cat_id']);
                        $top = intval($_POST['top']);
                        $hot = intval($_POST['hot']);
                        $active = intval($_POST['active']);
                        $db->query("UPDATE question SET title='$title',link='$link',cat_id=$cat_id,top=$top,hot=$hot,active=$active, question='$question' WHERE id=$id");
                        $_SESSION['rsMessages'] = 'Update question successful!';
                    }
                    include_once $config ['admin_document_root'] . "/modules/question_detail.php";
                    die;
                case 'viewAnswer':
                    include_once $config ['admin_document_root'] . "/modules/question_view_answer.php";
                    die;
                default:
                    $this->getAllQuestion($config ['total_article_on_admin']);
            }
        }
    }

    function deleteQuestion($id) {
        global $db, $config;
        $SQL = "DELETE FROM question WHERE id=" . intval($id);
        $sqlView = "SELECT * FROM question_attach WHERE question_id=" . intval($id);
        $rsEdit = $db->query($sqlView);
        $str = "";
        while ($row = $db->fetch_array($rsEdit)) {
            $file = $config['upload_dir'] . $row['filename'];
            if (file_exists($file)) {
                unlink($file);
            }
        }
        $db->query($SQL);
        $_SESSION['rsMessages'] = "<span class='success'>Delete question success!</span>" . $str;
    }

    function deleteAnswer($id) {
        global $db, $config;
        $SQL = "DELETE FROM answer WHERE id=" . intval($id);
        $db->query($SQL);
        $_SESSION['rsMessages'] = "<span class='success'>Delete answer success!</span>" . $str;
    }

    function viewAnswer($question_id) {
        global $db, $config;

        $sqlQuestion = "SELECT * FROM question WHERE id=$question_id";
        $rsQuestion = $db->query_first($sqlQuestion);

        if (isset($_GET['page'])) {
            $page = intval($_GET['page']);
        } else {
            $page = 1;
        }
        $sqlTotal = "SELECT count(*) as total FROM answer WHERE qid=$question_id";
        $rsTotal = $db->query_first($sqlTotal);
        $totalRows = $rsTotal['total'];
        $totalPages = ceil($totalRows / $config ['total_article_on_admin']);
        $start = (($page * $config ['total_article_on_admin']) - $config ['total_article_on_admin']);
        $limit = $config ['total_article_on_admin'];
        $sqlAnswer = "SELECT * FROM answer WHERE qid=$question_id ORDER BY create_date asc LIMIT $start,$limit";
        $rsAnswer = $db->query($sqlAnswer);

        return array('rsQuestion' => $rsQuestion, 'rsAnswer' => $rsAnswer, 'page' => $page, 'totalRow' => $totalRows, 'totalPage' => $totalPages);
    }

    function editAnswer($question_id) {
        global $db, $config;
        if ($_POST['updateAnswer'] == 1) {
            $id = trim($_POST['answer_id']);
            $title = trim($_POST['title']);
            $answer = trim($_POST['answer']);
            $db->query("UPDATE answer SET title='$title', answer='$answer' WHERE id=$id");
            $_SESSION['rsMessages'] = 'Update answer successful!';
        }
        $sql = "SELECT * FROM answer WHERE id=$question_id";
        $rs = $db->query_first($sql);
        return $rs;
    }
    
    function countAnswerInQuestion($question_id){
        global $db, $config;
        $sqlNotActive = "SELECT count(*) as total FROM answer WHERE qid=$question_id AND active=0";
        $rsNotActive = $db->query_first($sqlNotActive);
        $sqlActive = "SELECT count(*) as total FROM answer WHERE qid=$question_id AND active=1";
        $rsActive = $db->query_first($sqlActive);
        return array('totalNotActive'=>$rsNotActive['total'],'totalActive'=>$rsActive['total']);
    }

}

?>